<template>
  <div class="driver-manage">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="司机姓名">
          <el-input v-model="searchForm.name" placeholder="请输入司机姓名" clearable />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="searchForm.phone" placeholder="请输入联系电话" clearable />
        </el-form-item>
        <el-form-item label="工作状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>搜索
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 列表区域 -->
    <el-card class="list-card">
      <template #header>
        <div class="card-header">
          <span>司机列表</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>新增司机
          </el-button>
        </div>
      </template>

      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="name" label="司机姓名" width="120" />
        <el-table-column prop="phone" label="联系电话" width="120" />
        <el-table-column prop="vehicleNo" label="车牌号" width="120" />
        <el-table-column prop="vehicleType" label="车辆类型" width="120" />
        <el-table-column prop="status" label="工作状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="taskCount" label="今日任务" width="100" align="center" />
        <el-table-column prop="joinTime" label="入职时间" width="180" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="primary" @click="handleDetail(row)">详情</el-button>
            <el-button 
              link 
              :type="row.status === 'active' ? 'danger' : 'success'"
              @click="handleToggleStatus(row)"
            >
              {{ row.status === 'active' ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
      @close="resetForm"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item label="司机姓名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入司机姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="formData.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="车牌号" prop="vehicleNo">
          <el-input v-model="formData.vehicleNo" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车辆类型" prop="vehicleType">
          <el-select v-model="formData.vehicleType" placeholder="请选择车辆类型">
            <el-option
              v-for="item in vehicleTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="驾驶证照片" prop="licensePhoto">
          <el-upload
            class="upload-demo"
            action="#"
            :auto-upload="false"
            :limit="1"
            list-type="picture-card"
          >
            <template #default>
              <el-icon><Plus /></el-icon>
            </template>
            <template #file="{ file }">
              <div>
                <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
              </div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formData.remark"
            type="textarea"
            rows="3"
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitting">
          确定
        </el-button>
      </template>
    </el-dialog>

    <!-- 司机详情抽屉 -->
    <el-drawer
      v-model="drawerVisible"
      :title="`司机详情 - ${currentDriver.name}`"
      size="60%"
      destroy-on-close
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="司机姓名">{{ currentDriver.name }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ currentDriver.phone }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ currentDriver.idCard }}</el-descriptions-item>
        <el-descriptions-item label="车牌号">{{ currentDriver.vehicleNo }}</el-descriptions-item>
        <el-descriptions-item label="车辆类型">{{ currentDriver.vehicleType }}</el-descriptions-item>
        <el-descriptions-item label="工作状态">
          <el-tag :type="getStatusType(currentDriver.status)">
            {{ getStatusText(currentDriver.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="入职时间">{{ currentDriver.joinTime }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ currentDriver.remark }}</el-descriptions-item>
      </el-descriptions>

      <el-divider>配送统计</el-divider>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-card shadow="hover">
            <template #header>
              <div class="stat-header">
                <span>今日配送</span>
                <el-tag type="success">{{ currentDriver.todayStats?.count || 0 }}单</el-tag>
              </div>
            </template>
            <div class="stat-content">
              <div class="stat-item">
                <span class="label">完成订单</span>
                <span class="value">{{ currentDriver.todayStats?.completed || 0 }}</span>
              </div>
              <div class="stat-item">
                <span class="label">配送中</span>
                <span class="value">{{ currentDriver.todayStats?.delivering || 0 }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card shadow="hover">
            <template #header>
              <div class="stat-header">
                <span>本月配送</span>
                <el-tag type="warning">{{ currentDriver.monthStats?.count || 0 }}单</el-tag>
              </div>
            </template>
            <div class="stat-content">
              <div class="stat-item">
                <span class="label">准时率</span>
                <span class="value">{{ currentDriver.monthStats?.onTimeRate || '0%' }}</span>
              </div>
              <div class="stat-item">
                <span class="label">好评率</span>
                <span class="value">{{ currentDriver.monthStats?.goodRate || '0%' }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card shadow="hover">
            <template #header>
              <div class="stat-header">
                <span>总计配送</span>
                <el-tag type="info">{{ currentDriver.totalStats?.count || 0 }}单</el-tag>
              </div>
            </template>
            <div class="stat-content">
              <div class="stat-item">
                <span class="label">总里程</span>
                <span class="value">{{ currentDriver.totalStats?.mileage || 0 }}km</span>
              </div>
              <div class="stat-item">
                <span class="label">总时长</span>
                <span class="value">{{ currentDriver.totalStats?.hours || 0 }}h</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <el-divider>最近配送</el-divider>
      <el-table :data="currentDriver.recentDeliveries" border>
        <el-table-column prop="taskNo" label="任务编号" width="180" />
        <el-table-column prop="date" label="配送日期" width="120" />
        <el-table-column prop="orderCount" label="订单数" width="100" align="center" />
        <el-table-column prop="area" label="配送区域" min-width="200" show-overflow-tooltip />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getDeliveryStatusType(row.status)">
              {{ getDeliveryStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance } from 'element-plus'
import { Search, Refresh, Plus } from '@element-plus/icons-vue'

// 搜索表单
const searchForm = ref({
  name: '',
  phone: '',
  status: ''
})

// 表格相关
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 弹窗相关
const dialogVisible = ref(false)
const isAdd = ref(true)
const submitting = ref(false)
const formRef = ref<FormInstance>()

// 抽屉相关
const drawerVisible = ref(false)
const currentDriver = ref<any>({})

const formData = ref({
  name: '',
  phone: '',
  idCard: '',
  vehicleNo: '',
  vehicleType: '',
  licensePhoto: '',
  remark: ''
})

const formRules = {
  name: [{ required: true, message: '请输入司机姓名', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  idCard: [{ required: true, message: '请输入身份证号', trigger: 'blur' }],
  vehicleNo: [{ required: true, message: '请输入车牌号', trigger: 'blur' }],
  vehicleType: [{ required: true, message: '请选择车辆类型', trigger: 'change' }]
}

// 选项数据
const statusOptions = [
  { value: 'active', label: '在岗' },
  { value: 'delivering', label: '配送中' },
  { value: 'resting', label: '休息中' },
  { value: 'inactive', label: '已禁用' }
]

const vehicleTypeOptions = [
  { value: 'van', label: '面包车' },
  { value: 'truck', label: '货车' },
  { value: 'miniTruck', label: '小型货车' }
]

// 模拟数据
const tableData = ref([
  {
    id: '1',
    name: '张三',
    phone: '13800138000',
    idCard: '110101199001011234',
    vehicleNo: '京A12345',
    vehicleType: 'van',
    status: 'active',
    taskCount: 3,
    joinTime: '2023-01-01',
    remark: '优秀司机',
    todayStats: {
      count: 5,
      completed: 3,
      delivering: 2
    },
    monthStats: {
      count: 150,
      onTimeRate: '98%',
      goodRate: '95%'
    },
    totalStats: {
      count: 1580,
      mileage: 15800,
      hours: 960
    },
    recentDeliveries: [
      {
        taskNo: 'PS20231128001',
        date: '2023-11-28',
        orderCount: 5,
        area: '北京市朝阳区',
        status: 'completed'
      }
    ]
  }
])

const dialogTitle = computed(() => (isAdd.value ? '新增司机' : '编辑司机'))

// 方法
const getStatusType = (status: string) => {
  const map: Record<string, string> = {
    active: 'success',
    delivering: 'primary',
    resting: 'info',
    inactive: 'danger'
  }
  return map[status] || 'info'
}

const getStatusText = (status: string) => {
  const map: Record<string, string> = {
    active: '在岗',
    delivering: '配送中',
    resting: '休息中',
    inactive: '已禁用'
  }
  return map[status] || '未知状态'
}

const getDeliveryStatusType = (status: string) => {
  const map: Record<string, string> = {
    pending: 'warning',
    delivering: 'primary',
    completed: 'success'
  }
  return map[status] || 'info'
}

const getDeliveryStatusText = (status: string) => {
  const map: Record<string, string> = {
    pending: '待配送',
    delivering: '配送中',
    completed: '已完成'
  }
  return map[status] || '未知状态'
}

// ... 其他方法与 task.vue 类似 ...

</script>

<style scoped lang="scss">
.driver-manage {
  .search-card {
    margin-bottom: 20px;
    
    .search-form {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      
      .el-form-item {
        margin-bottom: 0;
        margin-right: 0;
      }
    }
  }
  
  .list-card {
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
  
  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }

  .stat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .stat-content {
    .stat-item {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      .label {
        color: #909399;
      }
      
      .value {
        font-weight: bold;
        color: #303133;
      }
    }
  }
}

// 响应式处理
@media screen and (max-width: 768px) {
  .driver-manage {
    .search-form {
      .el-form-item {
        width: 100%;
      }
    }
  }
}
</style> 